package com.yh.hljapp.gddb.gbmanager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.yh.hljapp.AppContext;
import com.yh.hljapp.common.StringUtils;
import com.yh.hljapp.gddb.gdbean.DaoMaster;
import com.yh.hljapp.gddb.gdbean.DaoSession;
import com.yh.hljapp.gddb.gdbean.MatHead;
import com.yh.hljapp.gddb.gdbean.MatHeadDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：MatHeadManager2
 * <p>
 * 创建时间：2017.06.02.17:01
 * *****************************
 */
public class MatHeadManager2 {

    private final static String dbName = "fb_db";
    private static MatHeadManager2 mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;
    private final DaoSession mDaoSession;

    public MatHeadManager2(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        mDaoSession = daoMaster.newSession();
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static MatHeadManager2 getInstance(Context context) {
        if (mInstance == null) {
            synchronized (MatHeadManager2.class) {
                if (mInstance == null) {
                    mInstance = new MatHeadManager2(context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }

    public void inOrRe(MatHead head) {
        MatHeadDao matHeadDao = mDaoSession.getMatHeadDao();
        matHeadDao.insertOrReplace(head);
    }

    //获得最后一个插入的
    public MatHead getLastHead() {
        MatHeadDao matHeadDao = mDaoSession.getMatHeadDao();
        List<MatHead> matHeads = matHeadDao.loadAll();
        int size = matHeads.size();
        return matHeads.get(size - 1);
    }

    public List<MatHead> getAll() {
        MatHeadDao matHeadDao = mDaoSession.getMatHeadDao();
        List<MatHead> matHeads = matHeadDao.loadAll();
        return matHeads;
    }

    //通过id拿到对应的唯一的实体
    public MatHead getHeadById(String id, String type) {
        MatHeadDao matHeadDao = mDaoSession.getMatHeadDao();
        MatHead unique = matHeadDao.queryBuilder().where(MatHeadDao.Properties.Id.eq(id), MatHeadDao.Properties.Ftype.eq(type)).unique();
        return unique;
    }

    public List<MatHead> getHeadsByNa(String name, String type) {

        MatHeadDao matHeadDao = mDaoSession.getMatHeadDao();

        List<MatHead> list = matHeadDao.queryBuilder().where(MatHeadDao.Properties.UserName.eq(name), MatHeadDao.Properties.Ftype.eq(type)).list();
        return list;

    }

    public List<MatHead> getHeadByKHNa(String userName, String khName, String type) {
        MatHeadDao matHeadDao = mDaoSession.getMatHeadDao();
//        QueryBuilder<MatHead> builder1 = matHeadDao.queryBuilder();
//        builder1.where(MatHeadDao.Properties.GhdwName.like("%" + khName + "%"), MatHeadDao.Properties.UserName.eq(userName), MatHeadDao.Properties.Ftype.eq(type));
//        List<MatHead> list = builder1.list();
//        return list;
        QueryBuilder<MatHead> builder = matHeadDao.queryBuilder();
        List<MatHead> list = builder.where(MatHeadDao.Properties.UserName.eq(userName), MatHeadDao.Properties.Ftype.eq(type)).list();
        List<MatHead> temList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            MatHead head = list.get(i);
            String ghdwName = head.getGhdwName();
            if (!StringUtils.isEmpty(ghdwName)) {
                if (ghdwName.contains(khName)) {
                    temList.add(head);
                }
            }
        }
        return temList;
    }

    public void delAllById(long id) {
        String name = AppContext.getInstance().getCurrentUser().getName();
        MatHeadDao headBeanDao = mDaoSession.getMatHeadDao();
        List<MatHead> list = headBeanDao.queryBuilder().where(MatHeadDao.Properties.UserName.eq(name), MatHeadDao.Properties.Id.eq(id)).list();
        headBeanDao.deleteInTx(list);
    }

}
